package carrier.controller;


import carrier.service.CarrierService;
import com.example.entity.*;
import com.example.utils.*;
import com.example.entity.Carrier;
import com.example.entity.Truck;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RefreshScope
public class CarrierController {

    @Resource
    CarrierService carrierService;

    @Resource
    PasswordEncoder passwordEncoder;

    ResultCode resultCode = new ResultCode();

    @GetMapping("/getCarrier")
//    @PreAuthorize("hasAnyAuthority('admin','account','carrier','driver')")
    public Result findCarrier(@RequestParam(required = false,name = "id") Long id){
        Result res = new Result();
        Map<String, Object> map = new HashMap<String, Object>();
        try {
            Carrier c = carrierService.findCarrier(id);
            map.put("carrierInfo",c);
            res.setMessage("返回成功");
            res.setData(map);
        } catch (Exception e) {
            e.printStackTrace();
            res.setCode(resultCode.InfoGetERR);
            res.setMessage(resultCode.getMsg(resultCode.InfoGetERR));
        }
        return res;
    }

    //承运商列表
    @GetMapping("/carrierList")
//    @PreAuthorize("hasAnyAuthority('admin','account','carrier')")
    public Result getCarrierList(@RequestParam(required = false,name = "name") String name){
        Result res = new Result();
        Map<String, Object> map = new HashMap<String, Object>();
        try {
            List<Carrier> list = carrierService.getInfo(name);
            map.put("carrierList",list);
            res.setMessage("返回成功");
            res.setData(map);
        } catch (Exception e) {
            e.printStackTrace();
            res.setCode(resultCode.InfoGetERR);
            res.setMessage(resultCode.getMsg(resultCode.InfoGetERR));
        }
        return res;
    }


    // 更新承运商信息
    @PutMapping("/modCarrier")
//    @PreAuthorize("hasAnyAuthority('admin','carrier')")
    public Result modCarrier(@RequestBody Carrier c){
        Result res = new Result();
        try {
            int r = carrierService.update(c);
            if(r > 0){
                res.setMessage("信息更新成功");
            }else{
                res.setCode(ResultCode.InfoModERR);
                res.setMessage(resultCode.getMsg(ResultCode.InfoModERR));
            }
        } catch (Exception e) {
            e.printStackTrace();
            res.setCode(ResultCode.InfoModERR);
            res.setMessage(resultCode.getMsg(ResultCode.InfoModERR));
        }
        return res;
    }

    //有空的司机列表
    @GetMapping("/driverList")
//    @PreAuthorize("hasAnyAuthority('carrier')")
    public Result getFreeDriver(@RequestParam(required = false,name = "id") Long id){
        Result res = new Result();
        Map<String, Object> map = new HashMap<String, Object>();
        try {
            List<User> list = carrierService.findFreeDrivers(id);
            map.put("driverList",list);
            res.setMessage("返回有空闲的司机列表");
            res.setData(map);
        } catch (Exception e) {
            e.printStackTrace();
            res.setCode(resultCode.InfoGetERR);
            res.setMessage(resultCode.getMsg(resultCode.InfoGetERR));
        }
        return res;
    }

    //有空的货车列表
    @GetMapping("/truckList")
//    @PreAuthorize("hasAnyAuthority('carrier')")
    public Result getFreeTruck(@RequestParam(required = false,name = "id") Long id){
        Result res = new Result();
        Map<String, Object> map = new HashMap<String, Object>();
        try {
            List<Truck> list = carrierService.findFreeTrucks(id);
            map.put("driverList",list);
            res.setMessage("返回有空闲的货车");
            res.setData(map);
        } catch (Exception e) {
            e.printStackTrace();
            res.setCode(resultCode.InfoGetERR);
            res.setMessage(resultCode.getMsg(resultCode.InfoGetERR));
        }
        return res;
    }

    @PostMapping("/addDriver")
//    @PreAuthorize("hasAnyAuthority('admin','carrier')")
    public Result addDriver(@Validated @RequestBody Map<String, Object> params){
        Long carrierId = Long.parseLong(params.get("carrierId").toString());
        String userName = (String) params.get("userName");
        String realName = (String) params.get("realName");
        String password = (String) params.get("password");
        password = passwordEncoder.encode(password);
        String phone = (String) params.get("phone");
        String email = (String) params.get("email");
        System.out.println("addDriver");
        Result res = new Result();
        try {
            carrierService.addDriver(carrierId,userName,realName,password,4,phone,email);
            res.setMessage("添加司机成功");
        } catch (Exception e) {
            e.printStackTrace();
            res.setCode(resultCode.InfoAddERR);
            res.setMessage(resultCode.getMsg(resultCode.InfoAddERR));
        }
        return res;
    }

    @PostMapping("/addTruck")
//    @PreAuthorize("hasAnyAuthority('admin','carrier')")
    public Result addTruck(@RequestBody Truck t){
        System.out.println("addTruck");
        Result res = new Result();
        try {
            carrierService.addTruck(t);
            res.setMessage("新增货车成功");
        } catch (Exception e) {
            e.printStackTrace();
            res.setCode(resultCode.InfoAddERR);
            res.setMessage(resultCode.getMsg(resultCode.InfoAddERR));
        }
        return res;
    }

    @GetMapping("/getUnitPrice")  // 根据id获取单价
    Float getUnitPrice(@RequestParam(name = "carrierId") Long carrierId) {
        return carrierService.getUnitPrice(carrierId);
    }

    @GetMapping("/selectTruckById")  // 根据id获取货车
    Truck selectTruckById(@RequestParam(name = "truckId") Long truckId){
        return carrierService.selectTruck(truckId);
    }

    @PutMapping("/updateTruckById")  // 根据id获取货车
    void updateTruckById(@RequestBody Truck truck){
        carrierService.updateTruck(truck);
    }

    @PutMapping("/updateDriverById")  // 更新司机状态
    void updateDriverState(@RequestParam(required = false,name = "state")Integer state,
                           @RequestParam(required = false,name = "carrierId")Long carrierId,
                           @RequestParam(required = false,name = "driverId")Long driverId){
        carrierService.updateDriver(state,carrierId,driverId);
    }

    @GetMapping("/selectCarrierById")  // 根据id获取承运商
    Carrier selectCarrierById(@RequestParam(name = "carrierId") Long carrierId){
        return carrierService.findCarrier(carrierId);
    }

    @GetMapping("/selectCarrierByUser")  // 根据用户id获取承运商
    Carrier selectCarrierByUser(@RequestParam(name = "id") Long id){
        return carrierService.findCarrierByUser(id);
    }


}
